########################################
###      Yankelovich 2024 Bovitz     ###
###            Run 2-wave Study      ###
###            Mac Lockhart          ###
###     Run after merge_waves.R      ###
###        Created: 10/25/2024       ###
###        Updated: 10/25/2024       ###
########################################

library(readr)
library(tidyverse)
library(dplyr)
library(kableExtra)
library(stargazer)
library(data.table)

# Header -----------------------------------------------------------------------

if(Sys.info()[7] == "laura"){
  setwd("/Users/laura/Dropbox/RA/MIT Evolving Election Administration Landscape/National vs State Trust in Elections/")
}
if(Sys.info()[7]=="ml2999"){
  setwd("E:/Dropbox/School/RA Work/'22 Seth & Thad/MIT Evolving Election Administration Landscape/National vs State Trust in Elections/")
}

DF <- fread("2_data/wave2_clean.csv")
DF$treatment <- factor(DF$treatment, levels = c("National", "State"))
DF$stateQ <- factor(DF$stateQ, levels = c("National", "Iowa", "California", "Idaho"))
DF$stateMatch <- (DF$stateQ=="California" & DF$state=="CA") | (DF$stateQ=="Iowa" & DF$state=="IA") | (DF$stateQ=="Idaho" & DF$state=="ID") 

########################
#Main analyses
########################

targetstate <- lm(state_outcome ~ treatment, DF)
own <- lm(ownvote_w2post  ~ treatment, DF)
local <- lm(localvote_w2post  ~ treatment, DF)
state <- lm(statevote_w2post  ~ treatment, DF)
country <- lm(usavote_w2post  ~ treatment, DF)
pollworker <- lm(pollworker_w2post  ~ treatment, DF)
votelikely <- lm(vote24_w2post  ~ treatment, DF)

stargazer(targetstate,
          own, local, state, country,
          pollworker, votelikely,
          type = "html",
          out = "4_visuals/SVN_MainTable.html",
          title = "Effect of state vs national messages on pre-registered outcomes in same wave as treatment was delivered.",
          column.labels = c("Target State (1-4)","Own Vote (1-4)", "Local Vote (1-4)", 
                            "State Vote (1-4)", "National Vote (1-4)",
                            "Poll Worker (1/0)", "Vote Likelihood (1-5)"),
          #dep.var.labels = c("Trust Measures"),
          dep.var.labels.include = F,
          covariate.labels = c("T2", "Constant"),
          omit.stat = c("f","adj.rsq", "ser"))

########################
#Change analyses
########################

targetstate <- lm(state_outcomechange ~ treatment, DF)
own <- lm(ownvotechange  ~ treatment, DF)
local <- lm(localchange  ~ treatment, DF)
state <- lm(statechange  ~ treatment, DF)
country <- lm(countrychange  ~ treatment, DF)
pollworker <- lm(pollworkerchange  ~ treatment, DF)
votelikely <- lm(votelikelychange  ~ treatment, DF)

stargazer(targetstate,
          own, local, state, country,
          pollworker, votelikely,
          type = "html",
          out = "4_visuals/SVN_ChangeTable.html",
          title = "Effect of state vs national messages on change in outcomes in same wave as treatment was delivered. Change not available for main outcome variable (column 1).",
          column.labels = c("Target State (1-4)","Own Vote (1-4)", "Local Vote (1-4)", 
                            "State Vote (1-4)", "National Vote (1-4)",
                            "Poll Worker (1/0)", "Vote Likelihood (1-5)"),
          #dep.var.labels = c("Trust Measures"),
          dep.var.labels.include = F,
          covariate.labels = c("State Video", "Constant"),
          omit.stat = c("f","adj.rsq", "ser"))

########################
#OwnState analyses
########################
targetstate <- lm(state_outcome ~ treatment*stateMatch, DF)
own <- lm(ownvote_w2post  ~ treatment*stateMatch, DF)
local <- lm(localvote_w2post  ~ treatment*stateMatch, DF)
state <- lm(statevote_w2post  ~ treatment*stateMatch, DF)
country <- lm(usavote_w2post  ~ treatment*stateMatch, DF)
pollworker <- lm(pollworker_w2post  ~ treatment*stateMatch, DF)
votelikely <- lm(vote24_w2post  ~ treatment*stateMatch, DF)

stargazer(targetstate,
          own, local, state, country,
          pollworker, votelikely,
          type = "html",
          out = "4_visuals/SVN_StateMatchTable.html",
          title = "Effect of state vs national messages on pre-registered outcomes in same wave as treatment was delivered.",
          column.labels = c("Target State (1-4)","Own Vote (1-4)", "Local Vote (1-4)", 
                            "State Vote (1-4)", "National Vote (1-4)",
                            "Poll Worker (1/0)", "Vote Likelihood (1-5)"),
          #dep.var.labels = c("Trust Measures"),
          dep.var.labels.include = F,
          covariate.labels = c("State Video", "Assigned Own State", "Assigned Own X State Video", "Constant"),
          omit.stat = c("f","adj.rsq", "ser"))